<?php
/**
 * @ClassName: Auth
 * @Description: 登录认证
 * @author: wyaohu
 * @Date: 2024/9/8 19:25
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;
use app\common\utils\GenQrcode;

class Auth extends Api
{
    protected $memberModel;

    public function initialize()
    {
        parent::initialize();
        $this->memberModel = new model\Member();
    }

    function memberInfo()
    {
        $token = input('token');
        $user_role = input('user_role', 1);
        $data = $this->userInfo;

        // 上级推广员
        $superior_field = 'id,username,nickname,real_name,mobile,avatar';
        $superior = model\Member::where('id', $data['first_leader'])->field($superior_field)->find();
        if (!$superior) {
            $first_leader = env('first_leader');
            if (!in_array($data['id'], [$first_leader])) {
                // 不存在上级推广员，设置默认推广员
                model\Member::where('id', $data['id'])->update(['first_leader' => $first_leader]);
                $superior = model\Member::where('id', $first_leader)->field($superior_field)->find();
            }
        }
        if ($superior) {
            if ($superior['real_name']) {
                $superior['nickname'] = $superior['nickname'] . '(' . $superior['real_name'] . ')';
            }
            $superior['avatar'] = replace_url($superior['avatar']);
        }
        $data['superior'] = $superior;

        // 用户头像
        $data['avatar'] = replace_url($data['avatar']);

        // 用户等级
//        $data['level_name'] = '黄金会员';

        // 代理等级
//        $data['agent_level_name'] = 'J8';

        // 招商等级
//        $data['brand_level_name'] = 'Z5';

        // 运营中心等级
//        $data['operation_level_name'] = '省级运营中心';

        // 优惠券
        $data['coupon'] = 0;

        // 水票、门票
        $data['water_tickets'] = 0;
        $data['door_tickets'] = 0;

        $role_list = [[
            'id' => 1,
            'name' => '个人用户',
        ]];
        if ($user_role == 1) {
            $cus_where = ' c.customer_type = 1 ';
        } else {
            $cus_where = ' c.customer_type in (2, 3) ';
        }
        $data['customer_id'] = 0;
        $data['customer'] = [];
        // 是不是客户联系人
        $contacts_list = model\CustomerContacts::alias('cc')
            ->leftJoin('customer c', 'c.id=cc.customer_id')
            ->where('cc.member_id', $data['id'])
            ->field('cc.*,c.customer_type,c.customer_name')
            ->select();
        if (count($contacts_list) > 0) {
            $customer_id = 0;
            foreach ($contacts_list as $item) {
                if (in_array($item['customer_type'], [2, 3])) {
                    $role_list[] = [
                        'value' => 2,
                        'text' => '企业用户',
                        'disable' => false,
                    ];
                    break;
                }
            }
            $contacts = model\CustomerContacts::alias('cc')
                ->leftJoin('customer c', 'c.id=cc.customer_id')
                ->where('cc.member_id', $data['id'])
                ->where($cus_where)
                ->field('cc.*,c.customer_type,c.customer_name')
                ->find();
            $sql = model\CustomerContacts::getLastSql();
            if ($contacts) {
                $customer_id = $contacts['customer_id'];
            }
            $data['customer_id'] = $customer_id;
            $data['customer'] = model\Customer::where('id', $customer_id)->find();
        }
        $data['role_list'] = $role_list;

        return json(['code' => 1, 'msg' => 'OK', 'data' => $data]);
    }

    function createCodeImg()
    {
        $member_id = $this->member_id;
        $invite_type = input('type/d', 0);
        $invite_store = input('store_id/d', 0);
        $res = GenQrcode::createCodeImg($member_id, $invite_type, $invite_store);

        return json(['code' => 1, 'msg' => '保存成功', 'data' => $res]);
    }

}